```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java自定义异常类指南 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e48aa 0%, #9d50bb 100%);
        }
        .code-block {
            background: #2d2d2d;
            border-left: 4px solid #9d50bb;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        .highlight {
            position: relative;
        }
        .highlight:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 30%;
            background-color: rgba(157, 80, 187, 0.2);
            z-index: -1;
            transform: scaleX(1.02);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl text-center">
            <div class="flex justify-center mb-6">
                <span class="px-4 py-1 bg-white bg-opacity-20 rounded-full text-sm font-medium">Java异常处理</span>
            </div>
            <h1 class="text-4xl md:text-5xl font-bold mb-6">掌握<span class="highlight">Java自定义异常类</span>的艺术</h1>
            <p class="text-xl opacity-90 max-w-3xl mx-auto mb-8">创建清晰、优雅的错误处理机制，提升代码可维护性与用户体验</p>
            <div class="flex justify-center space-x-4">
                <a href="#intro" class="px-6 py-3 bg-white text-purple-800 font-medium rounded-lg hover:bg-opacity-90 transition">开始探索</a>
                <a href="#principles" class="px-6 py-3 border border-white text-white font-medium rounded-lg hover:bg-white hover:bg-opacity-10 transition">设计原则</a>
            </div>
        </div>
    </section>

    <!-- Intro Section -->
    <section id="intro" class="py-16 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex items-center mb-10">
                <div class="w-2 h-12 bg-purple-600 mr-4"></div>
                <h2 class="text-3xl font-bold">为什么需要自定义异常类？</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-16">
                <div>
                    <p class="text-lg mb-6">自定义异常类在 Java 中可以让你创建特定于应用程序的错误处理机制。它们用于捕获和处理应用程序中出现的特殊情况，这些情况无法通过 Java 标准库中的现有异常类来表示。</p>
                    <div class="bg-gray-100 p-6 rounded-lg border-l-4 border-purple-500">
                        <h3 class="font-bold text-lg mb-3 flex items-center">
                            <i class="fas fa-lightbulb text-purple-600 mr-2"></i>
                            关键优势
                        </h3>
                        <ul class="space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>精确表达业务领域特定的错误情况</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>提高代码可读性和维护性</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>支持更精细的错误处理逻辑</span>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="relative">
                    <div class="bg-white rounded-xl shadow-lg overflow-hidden card-hover">
                        <div class="p-6">
                            <h3 class="font-bold text-xl mb-4 flex items-center">
                                <i class="fas fa-code text-purple-600 mr-2"></i>
                                异常类型对比
                            </h3>
                            <div class="mermaid">
                                graph TD
                                    A[Throwable] --> B[Error]
                                    A --> C[Exception]
                                    C --> D[RuntimeException]
                                    C --> E[IOException]
                                    C --> F[自定义检查型异常]
                                    D --> G[自定义运行时异常]
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mb-16">
                <div class="flex items-center mb-8">
                    <div class="w-2 h-10 bg-purple-600 mr-4"></div>
                    <h2 class="text-2xl font-bold">创建自定义异常类</h2>
                </div>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                        <div class="p-6">
                            <h3 class="font-bold text-xl mb-4 flex items-center text-purple-600">
                                <i class="fas fa-check-circle mr-2"></i>
                                检查型异常
                            </h3>
                            <p class="mb-4">继承自 <code class="bg-gray-100 px-2 py-1 rounded">Exception</code> 类，需要在方法中显式声明或处理，适用于需要调用者显式处理的错误情况。</p>
                            <div class="code-block rounded-lg overflow-hidden mb-4">
                                <pre class="text-gray-200 p-4 text-sm"><code>// 检查型异常示例
public class CustomCheckedException extends Exception {
    public CustomCheckedException(String message) {
        super(message);
    }

    public CustomCheckedException(String message, Throwable cause) {
        super(message, cause);
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                        <div class="p-6">
                            <h3 class="font-bold text-xl mb-4 flex items-center text-red-500">
                                <i class="fas fa-running mr-2"></i>
                                运行时异常
                            </h3>
                            <p class="mb-4">继承自 <code class="bg-gray-100 px-2 py-1 rounded">RuntimeException</code> 类，不强制处理，适用于程序逻辑错误或不可恢复的错误情况。</p>
                            <div class="code-block rounded-lg overflow-hidden mb-4">
                                <pre class="text-gray-200 p-4 text-sm"><code>// 运行时异常示例
public class CustomRuntimeException extends RuntimeException {
    public CustomRuntimeException(String message) {
        super(message);
    }

    public CustomRuntimeException(String message, Throwable cause) {
        super(message, cause);
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-16 card-hover">
                <div class="p-6">
                    <h3 class="font-bold text-xl mb-4 flex items-center">
                        <i class="fas fa-cogs text-purple-600 mr-2"></i>
                        构造函数设计建议
                    </h3>
                    <div class="grid md:grid-cols-2 gap-6">
                        <div>
                            <h4 class="font-medium mb-2 text-purple-700">标准构造函数</h4>
                            <ul class="list-disc pl-5 space-y-2">
                                <li>无参构造函数：提供默认错误信息</li>
                                <li>带消息构造函数：允许传递自定义错误信息</li>
                                <li>带原因构造函数：支持异常链式调用</li>
                            </ul>
                        </div>
                        <div>
                            <h4 class="font-medium mb-2 text-purple-700">高级构造函数</h4>
                            <ul class="list-disc pl-5 space-y-2">
                                <li>带错误码构造函数：适合需要错误分类的系统</li>
                                <li>带上下文信息：提供更多调试信息</li>
                                <li>国际化支持：提供多语言错误消息</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Principles Section -->
    <section id="principles" class="py-16 px-4 md:px-0 bg-gray-100">
        <div class="container mx-auto max-w-5xl">
            <div class="flex items-center mb-10">
                <div class="w-2 h-12 bg-purple-600 mr-4"></div>
                <h2 class="text-3xl font-bold">自定义异常类设计原则</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-start mb-4">
                        <div class="bg-purple-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-bullseye text-purple-600 text-xl"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-xl mb-1">清晰的异常类型</h3>
                            <p class="text-gray-600">自定义异常类应该明确表示某种特定的错误情况。避免创建过于通用的异常类，这样可以提高异常处理的针对性。</p>
                        </div>
                    </div>
                    <div class="code-block rounded-lg overflow-hidden">
                        <pre class="text-gray-200 p-4 text-sm"><code>// 好的示例：特定业务异常
public class InsufficientFundsException extends RuntimeException {
    public InsufficientFundsException(double amount) {
        super("Insufficient funds for withdrawal of amount: " + amount);
    }
}

// 不好的示例：过于通用
public class BusinessException extends RuntimeException {
    // 过于笼统，无法明确错误类型
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-start mb-4">
                        <div class="bg-purple-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-project-diagram text-purple-600 text-xl"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-xl mb-1">继承适当的基类</h3>
                            <p class="text-gray-600">根据异常性质选择合适的父类：检查型异常继承Exception，运行时异常继承RuntimeException。</p>
                        </div>
                    </div>
                    <div class="mermaid">
                        flowchart TD
                            A[选择异常类型] --> B{是否需要强制处理}
                            B -->|是| C[继承Exception]
                            B -->|否| D[继承RuntimeException]
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-start mb-4">
                        <div class="bg-purple-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-comment-alt text-purple-600 text-xl"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-xl mb-1">提供有意义的错误消息</h3>
                            <p class="text-gray-600">异常消息应该能清楚地描述错误的性质，帮助开发人员理解问题的根本原因。</p>
                        </div>
                    </div>
                    <div class="code-block rounded-lg overflow-hidden">
                        <pre class="text-gray-200 p-4 text-sm"><code>// 好的示例：提供详细上下文信息
public class UserNotFoundException extends RuntimeException {
    public UserNotFoundException(String userId) {
        super("User with ID " + userId + " not found. " +
              "Please check if the ID is correct or the user exists.");
    }
}

// 不好的示例：信息过于简单
public class UserNotFoundException extends RuntimeException {
    public UserNotFoundException() {
        super("User not found");
    }
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-start mb-4">
                        <div class="bg-purple-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-stream text-purple-600 text-xl"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-xl mb-1">支持异常链式调用</h3>
                            <p class="text-gray-600">提供构造函数来传递导致异常的原始原因，这有助于追踪异常的根本原因。</p>
                        </div>
                    </div>
                    <div class="code-block rounded-lg overflow-hidden">
                        <pre class="text-gray-200 p-4 text-sm"><code>// 支持链式调用的异常类
public class FileProcessingException extends Exception {
    public FileProcessingException(String message, Throwable cause) {
        super(message, cause);
    }
}

// 使用示例
try {
    // 可能抛出IOException的代码
} catch (IOException e) {
    throw new FileProcessingException("Failed to process file", e);
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-start mb-4">
                        <div class="bg-purple-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-file-alt text-purple-600 text-xl"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-xl mb-1">文档化异常</h3>
                            <p class="text-gray-600">在文档中详细说明自定义异常的用途和触发条件，帮助其他开发人员理解如何处理它们。</p>
                        </div>
                    </div>
                    <div class="code-block rounded-lg overflow-hidden">
                        <pre class="text-gray-200 p-4 text-sm"><code>/**
 * 当用户尝试访问未授权的资源时抛出此异常。
 * 
 * &lt;p&gt;此异常通常在以下情况下抛出:
 * &lt;ul&gt;
 *   &lt;li&gt;用户没有所需权限&lt;/li&gt;
 *   &lt;li&gt;用户会话已过期&lt;/li&gt;
 *   &lt;li&gt;访问令牌无效&lt;/li&gt;
 * &lt;/ul&gt;
 * 
 * @see AuthenticationService
 */
public class UnauthorizedAccessException extends RuntimeException {
    public UnauthorizedAccessException(String message) {
        super(message);
    }
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-start mb-4">
                        <div class="bg-purple-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-ban text-purple-600 text-xl"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-xl mb-1">避免滥用异常</h3>
                            <p class="text-gray-600">自定义异常应仅用于处理实际的错误情况，而不是用于控制程序流。使用异常处理错误，而不是用于正常的程序流程控制。</p>
                        </div>
                    </div>
                    <div class="grid grid-cols-2 gap-4">
                        <div class="bg-red-50 p-3 rounded-lg border border-red-200">
                            <h4 class="font-medium text-red-700 mb-1">不好的做法</h4>
                            <p class="text-sm text-red-600">使用异常控制正常流程</p>
                            <pre class="text-xs mt-2"><code>try {
    if (!isValid(input)) {
        throw new ValidationException();
    }
} catch (ValidationException e) {
    // 处理无效输入
}</code></pre>
                        </div>
                        <div class="bg-green-50 p-3 rounded-lg border border-green-200">
                            <h4 class="font-medium text-green-700 mb-1">好的做法</h4>
                            <p class="text-sm text-green-600">使用条件语句处理正常流程</p>
                            <pre class="text-xs mt-2"><code>if (!isValid(input)) {
    showErrorMessage("Input is invalid");
    return;
}
// 继续处理有效输入</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Best Practices Section -->
    <section class="py-16 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex items-center mb-10">
                <div class="w-2 h-12 bg-purple-600 mr-4"></div>
                <h2 class="text-3xl font-bold">最佳实践与实用技巧</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h3 class="font-bold text-xl mb-3">异常层次结构</h3>
                    <p class="text-gray-600 mb-4">为你的应用程序设计合理的异常层次结构，创建基础异常类和更具体的子类。</p>
                    <div class="mermaid">
                        graph BT
                            A[ApplicationException] --> B[BusinessException]
                            A --> C[TechnicalException]
                            B --> D[ValidationException]
                            B --> E[PaymentException]
                            C --> F[DatabaseException]
                            C --> G[NetworkException]
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-tags"></i>
                    </div>
                    <h3 class="font-bold text-xl mb-3">错误码与类型</h3>
                    <p class="text-gray-600 mb-4">考虑在自定义异常中添加错误码和类型，便于客户端处理和国际化。</p>
                    <div class="code-block rounded-lg overflow-hidden">
                        <pre class="text-gray-200 p-4 text-sm"><code>public class ApiException extends RuntimeException {
    private final String errorCode;
    private final ErrorType errorType;
    
    public ApiException(String errorCode, ErrorType type, String message) {
        super(message);
        this.errorCode = errorCode;
        this.errorType = type;
    }
    
    // Getters...
}

public enum ErrorType {
    CLIENT_ERROR,
    SERVER_ERROR,
    VALIDATION_ERROR,
    AUTHENTICATION_ERROR
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="text-purple-600 text-3xl mb-4">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <h3 class="font-bold text-xl mb-3">安全考虑</h3>
                    <p class="text-gray-600 mb-4">在异常消息中避免暴露敏感信息，如密码、密钥或系统内部细节。</p>
                    <div class="grid gap-4">
                        <div class="bg-red-50 p-3 rounded-lg">
                            <h4 class="font-medium text-red-700 mb-1">不安全</h4>
                            <p class="text-sm">"登录失败: 密码 'secret123' 不匹配"</p>
                        </div>
                        <div class="bg-green-50 p-3 rounded-lg">
                            <h4 class="font-medium text-green-700 mb-1">安全</h4>
                            <p class="text-sm">"登录失败: 用户名或密码不正确"</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-10 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-sm">探索编程艺术，分享技术真知</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-purple-400 hover:text-purple-300 transition flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        <span>访问语雀专栏</span>
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-sm text-center text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```